
/*
 * Restore class
 * This class represents restoring a serialzied Greenhouse, applying the fix, and
 * starting off where where it errored.
 * 
 * James Zhang
 * ID: 3069886
 * July 17, 2012
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

public class Restore
{
    //Reference to the GUI
    private GreenhouseControlsUI gui;

    /**
     * Default Constructor
     * @param filename - Path of the serialized GreenhouseControls
     * @param g - The GUI which it outputs to
     */
    public Restore( String filename, GreenhouseControlsUI g )
    {
        gui = g;
        try
        {
            //Deserializes the object from said path
            FileInputStream file = new FileInputStream( new File( filename ) );
            ObjectInputStream restoreObj = new ObjectInputStream( file );
            GreenhouseControls restoreGc = ( GreenhouseControls ) restoreObj.readObject();
            restoreObj.close();
            restoreGc.gui = this.gui;

            //Prints state of the restored object
            restoreGc.getState();

            gui.jTextArea1.append( "\n" );
            gui.jTextArea1.append( "Applying proper fix...\n" );
            //Gets error code and applies appropriate fix
            restoreGc.getFixable( restoreGc.getError() );

            gui.jTextArea1.append( "\n" );
            gui.jTextArea1.append( "Starting events from error...\n" );
            gui.jTextArea1.append( "\n" );

            //Starting events again
            restoreGc.restoreEvent();

            //Sets the proper button states in GUI, etc
            gui.busy = false;
            gui.jButton2.setEnabled( false );
            gui.jButton1.setEnabled( false );
            if ( gui.filepath.equals( "" ) )
            {
                gui.jButton4.setEnabled( false );
            } else
            {
                gui.jButton4.setEnabled( true );
            }
        } catch ( IOException e )
        {
            e.printStackTrace();
        } catch ( ClassNotFoundException ex )
        {
            gui.jTextArea1.append( "Class was not found\n" );
        }
    }
}
